/**
 * Created by Mark on 2018/4/17.
 * 订单确认的处理逻辑
 */

require("./order-confirm.css");
require("page/common/nav/nav.js");
require("page/common/header/header.js");
var _mm = require("util/mm.js");
var _address = require("service/address-service.js");
var _order = require("service/order-service.js");
var _addressModel = require("./address-model.js");/* 收货地址的逻辑处理 */
var addressTemplateHtml = require("./address-list.string");/* 渲染收货地址的模板*/
var productTemplateHtml = require("./product-list.string");/* 渲染商品的模板 */


/**
 * Created by Mark on 2018/4/16.
 * 购物车的处理逻辑
 */

var page = {
    data: {
        selectAddressId : null//缓存选中的地址id，当有重新加载的将active属性回填
    },
    init: function () {
        this.onLoad();
        this.bindEvent();
    },
    onLoad : function(){
        this.loadAddressList();//加载收货地址列表
        this.loadProductList();//加载商品里列表
    },
    bindEvent : function(){
        this.clickAddress();//点击收货地址
        this.addAddress();//添加收货地址
        this.deleteAddress();//删除收货地址
        this.updateAddress();//编辑收货地址
        this.clickOrderSubmitBtn();//订单提交
    },
    //1.页面初始化函数
    loadAddressList : function(){//1.1加载收货地址列表
        var _this = this;
        _address.getAddressList(
            function (res) {
                _this.addressFilter(res.data)
                var  addressListHtml = _mm.renderHtml(addressTemplateHtml,res.data);
                $(".address-con").html(addressListHtml);
            },
            function (err) {
                $(".address-con").html('<p class="err-tip">地址加载失败，请刷新后重试</p>');
            }
        );

    },
    loadProductList : function(){//1.2加载商品里列表
        _order.getProductList(
            function (res) {
                var  productListHtml = _mm.renderHtml(productTemplateHtml,res.data);
                $(".product-con").html(productListHtml);
            }, function (err) {
                $('.product-con').html('<p class="err-tip">商品信息加载失败，请刷新后重试</p>');
        });
    },

    //2.事件绑定函数
    clickAddress : function () { //2.1点击收货地址追加样
        var _this = this;
        $(document).on("click",".address-item", function () {
            $(this).addClass("active").siblings(".address-item")
                .removeClass("active");
            _this.data.selectAddressId = $(this).data("id");//缓存选中地址的id
        });
    },
    addAddress : function(){//2.2新增收货地址
        var _this = this;
        $(document).on("click",".address-add", function () {
            _addressModel.show({
                isUpdate : false,
                onSuccess : function () {//回调函数
                    _this.loadAddressList();
                }
            });
        });
    },
    deleteAddress : function () {//2.3删除收货地址
        var _this = this;
        $(document).on("click",".address-delete", function (e) {
            e.stopPropagation();//阻止时间冒泡
            if(window.confirm("确定要删除改地址吗？")){
                var shippingId = $(this).parents(".address-item").data("id");
                _address.deleteAddress(shippingId,
                    function () {
                        _this.loadAddressList();
                }, function (err) {
                        _mm.errorTips(err.msg);
                });
            }
        });
    },
    updateAddress : function () {//2.4编辑收货地址
        var _this = this;
        $(document).on("click",".address-update", function (e) {
            e.stopPropagation();//阻止时间冒泡
            var shippingId = $(this).parents(".address-item").data("id");
            _address.getAddress(shippingId, 
                function (res) {//成功的回调函数
                    _addressModel.show({
                        isUpdate : true,
                        data : res.data,
                        onSuccess : function () {
                            _this.loadAddressList();
                        }
                    });
            }, function (err) {
                    _mm.errorTips(err.msg);
                })
            
        });
    },
    clickOrderSubmitBtn : function(){//2.5订单提交
        var _this = this;
        $(document).on("click",".order-submit", function () {
            var shippingId = _this.data.selectAddressId;
            if(shippingId){
                _order.createOrder({
                    shippingId : shippingId
                }, function (res) {
                    var orderNo  = res.data.map(function (order) {
                        return order
                    }).join(",");
                    window.location.href = './payment.html?orderNumber=' + orderNo;
                }, function (err) {
                    _mm.errorTips(err.msg);
                });
            }else{
                _mm.errorTips("请选择收货地址在提交");
            }
        });
    },
    //3.辅助函数
    addressFilter : function (data) {//3.1标记选中状态
        if(this.data.selectAddressId){
            var selectAddressIdFlag = false;
            var i = 0
            for(i,iLength = data.list.length; i < iLength; i++){
                if(data.list[i].id === this.data.selectAddressId){
                    data.list[i].isActive = true;
                    selectAddressIdFlag = true;
                }
            };
            if(!selectAddressIdFlag){//如果以前选中的地址不在目前的列表里
                this.data.selectAddressId = null;
            }
        }
    },


};
$(function(){
    page.init();
});

